package com.example.aicooking.controller;

import com.example.aicooking.entity.UserDetail;
import com.example.aicooking.service.UserDetailService;
import com.example.aicooking.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/health")
public class UserDetailController {
    @Autowired
    private UserDetailService userDetailService;

    // 查询用户健康信息
    @GetMapping("/{userId}")
    public Result<UserDetail> getUserHealth(@PathVariable Long userId) {
        try {
            UserDetail userDetail = userDetailService.getUserDetail(userId);
            return Result.success(userDetail);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    // 添加详细信息
    @PostMapping("/add/{userId}")
    public Result<String> addUserHealth(
            @PathVariable Long userId,
            @RequestBody UserDetail userDetail) {
        try {
            userDetail.setUserId(userId);
            userDetailService.addUserDetail(userDetail);
            return Result.success("添加成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    // 更新健康信息
    @PostMapping("/update/{userId}")
    public Result<String> updateUserHealth(
            @PathVariable Long userId,
            @RequestBody UserDetail userDetail) {
        try {
            userDetail.setUserId(userId);
            userDetailService.updateUserDetail(userDetail);
            return Result.success("更新成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }


}
